package com.iot.manage.dao;

import com.alibaba.fastjson.JSONObject;
import com.iot.manage.pojo.SysCode;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface SysCodeMapper {

    @Select("<script>" +
            "SELECT  \n" +
            " temp.CODE_TYPE sysCodeType,  \n" +
            " temp.CODE_NAME codeName,  \n" +
            " temp.CODE_VALUE codeValue,\n" +
            " temp.CODE_DESC codeDesc\n" +
            "FROM (   \n" +
            "\tSELECT  \n" +
            "\t\tROWNUM rowno,  \n" +
            "\t\tt.*  \n" +
            "\tFROM T_SYS_CODE t\n" +
            "\tWHERE (t.CODE_TYPE LIKE CONCAT(CONCAT('%', #{query}),'%') " +
            "OR t.CODE_NAME LIKE CONCAT(CONCAT('%', #{query}),'%')" +
            "OR t.CODE_VALUE LIKE CONCAT(CONCAT('%', #{query}),'%')" +
            ") \n" +
            "\tAND ROWNUM &lt;= #{pagesize}  ORDER BY t.CODE_TYPE\n" +
            ") temp WHERE temp.rowno &gt; #{start}" +
            "</script>")
    List<SysCode> findSysCodeList(@Param("query") String query, @Param("start") int start, @Param("pagesize") int pagesize);

    @Select("<script>SELECT COUNT(1) FROM T_SYS_CODE a WHERE " +
            "a.CODE_TYPE LIKE CONCAT(CONCAT('%', #{query}),'%')\n" +
            "OR a.CODE_NAME LIKE CONCAT(CONCAT('%', #{query}),'%')\n" +
            "OR a.CODE_VALUE LIKE CONCAT(CONCAT('%', #{query}),'%')</script>")
    int countSysCodeList(String query);

    @Insert("<script>" +
            "INSERT INTO T_SYS_CODE \n" +
            "(CODE_TYPE,CODE_NAME,CODE_VALUE" +
            "<if test=\" codeDesc != null\">"+
            ",CODE_DESC" +
            "</if>"+
            ") \n" +
            "VALUES \n" +
            "(#{sysCodeType},#{codeName},#{codeValue}" +
            "<if test=\" codeDesc != null\">"+
            ",#{codeDesc}" +
            "</if>"+
            ")" +
            "</script>")
    void addSysCodeInfo(JSONObject params);

    @Select("<script>" +
            "SELECT  \n" +
            " temp.CODE_TYPE sysCodeType,  \n" +
            " temp.CODE_NAME codeName,  \n" +
            " temp.CODE_VALUE codeValue,\n" +
            " temp.CODE_DESC codeDesc\n" +
            "FROM T_SYS_CODE temp\n" +
            "\tWHERE temp.CODE_TYPE = #{sysCodeType} AND temp.CODE_NAME = #{codeName}\n" +
            "</script>")
    SysCode findSysCodeInfoBySysCodeTypeAndCodeName(@Param("sysCodeType") String sysCodeType, @Param("codeName") String codeName);

    @Update("<script>" +
            "UPDATE T_SYS_CODE SET \n" +
            "\tCODE_TYPE = #{sysCodeType},\n" +
            "\tCODE_NAME = #{codeName},\n" +
            "\tCODE_VALUE = #{codeValue}\n" +
            "<if test=\" codeDesc != null\">"+
            "\t,CODE_DESC = #{codeDesc}\n" +
            "</if>"+
            "WHERE (CODE_TYPE = #{oldSysCodeType} AND CODE_NAME = #{oldSysCodeName})\n" +
            "</script>")
    void updateSysCodeInfo(JSONObject params);

    @Delete("<script>DELETE FROM T_SYS_CODE WHERE CODE_TYPE = #{sysCodeType} AND CODE_NAME = #{codeName}</script>")
    void removeSysCodeInfo(@Param("sysCodeType") String sysCodeType, @Param("codeName") String codeName);
}
